<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/29
 * Time: 下午8:20
 */

class MarketController extends AdminBaseController {

    /**
     * 等级分布报表
     */
    public function transfer(){
        $this->setTitle(PubFunc::t('订阅用户等级分布报表'));

        $filter = new PubFilter();
        $grid   = new PubGrid();
        $chart = new PubChart();

//        if($_REQUEST['kpi'] != "Default"){
//            $_REQUEST['server'] = 'ALL';
//        }

        $kpiOptions = array(
            "Default"   	 => PubFunc::t("等级分布"),
            'SubRegRole'     => PubFunc::t('订阅并注册创角'),
            'SubRegNoRole'   => PubFunc::t('订阅注册未创角'),
            'SubNoReg'   	 => PubFunc::t('订阅但未注册'),
            'NoSubRegRole' 	 => PubFunc::t('未订阅但注册并创角'),
            'NoSubRegNoRole' => PubFunc::t('未订阅未创角但注册')
        );

        $stateOptions = array('ALL' => PubFunc::t('等级分布'));

        $filter->addPlatformFilter()
            ->addFilter('kpi', 'link', PubFunc::t('KPI指标'), array('options' => $kpiOptions, 'value' => 'Default', 'fixed'=>true))
            ->addFilter('state', 'hidden', '', array('value' => 'ALL'));
        $params = $filter->get();

        $params['platform'] = 'r2games';
        $params['game'] = 'st';
        $date = MarketService::getEndDate($params['game']);
        $params['date'] = $date;
        $params['order'] = $this->R('order');
        $params['kpi'] == "Default" || !array_key_exists($params['kpi'],$kpiOptions);
        if($params['kpi'] == "Default" || !array_key_exists($params['kpi'],$kpiOptions)){
            $this->setTitle(PubFunc::t('订阅用户等级分布报表') . "-" . PubFunc::t('截至') . MarketService::getEndDate($params['game']));
            $header = MarketService::levelHeader();
            $data   = MarketService::levelData($params);
            $chartConfig = MarketService::levelChart($data);
            $data = MarketService::addPercent($data);
            $chart->addChart('chart', $chartConfig['data'], array(
                'name'   => PubFunc::t("用户等级分布"),
                'type'   => 'bar',
            ));
            foreach($stateOptions as $k=>$v){
                if($k == $params['state']){
                    $grid->addGrid($header, $data, array(
                        'name'  => $v,
                        'order' => array(
                            array('RecDate', 'desc'),
                            array("RoleLevel",'asc'),
                        ),
                    ));
                }else{
                    $grid->addLink($k, $v);
                }
            }

            $this->out['filter'] = $filter->show();
            $this->out['grid'] = $grid->show();
            $this->out['chart'] = $chart->show();
        }
        else{
            $this->userDetail($params);
            $this->out['filter'] = $filter->show();
            $this->tpl = "market_detail.tpl";
        }
    }

    private function userDetail($params){
        $this->setTitle(PubFunc::t('市场预热转化数据') . "-" . PubFunc::t('截至') . MarketService::getEndDate($params['game']));

        $grid   = new PubGrid();


        $stateOptions = array('ALL' => PubFunc::t('预热转化'));

        $header = MarketService::detailHeader($params);
        $kpi = $params['kpi'];
        if($this->R('type') != 'json'){
            foreach($stateOptions as $k=>$v){
                if($k == $params['state']){
                    $grid->addGrid($header, "/ga/?r=market/transfer&kpi=${kpi}&type=json&p=".PubBase::getCurrentPlatform()."&g=".PubBase::getCurrentGame()."&server=".$params['server'], array(
                        'name'      => $v,
                        'length'    => 20,
                    ));
                }else{
                    $grid->addLink($k, $v);
                }
            }
            $this->out['grid'] = $grid->show();
        }else{
            $params['length'] = $this->R('length',20);
            $data   = MarketService::detailData($params);
            $count   = MarketService::detailData($params,true);
            $count   = intval($count[0]['num']);
            $result = array(
                'count'=> $count,
                'data'=>$data,
                'result'=>true,
            );
            $this->out = $result;
            $this->outType = 'json';
        }

    }

} 